<?php

require_once 'common.php';
require_once 'ResultData.php';
require_once 'Student.php';


if (isGet()) {
    echo "不得以GET访问";
    exit;
}
$result = new ResultData();

$size = trim($_REQUEST['size']); // 一页的数量

if ($size == "") {
    $result->code = "404";
    $result->msg = "size未填写";
    $json_string = json_encode($result);
    exit($json_string);
}
$size = intval($size);
$page = trim($_REQUEST['page']); // 第几页的数量

if ($page == "") {
    $result->code = "404";
    $result->msg = "page未填写";
    $json_string = json_encode($result);
    exit($json_string);
}
$page = intval($page);

$json_string = file_get_contents('data.json');

// 用参数true把JSON字符串强制转成PHP数组
$data = json_decode($json_string);

class Data
{
    public $count;
    public $list;
}

$list = new Data();
$result->data = $list;
$list->list = array();

$list->count = count($data);
$begin = ($page - 1) * $size;
$end = ($page - 1) * $size + $size;

foreach ($data as $key => $value) {

    if ($key >= $begin && $key < $end) {

        $student = new Student();
        $student->id = $value->id;
        $student->name = $value->name;
        $student->sex = $value->sex;
        $student->age = $value->age;
        $student->edu = $value->edu;
        $student->city = $value->city;
        $student->no = $value->no;

        array_push($list->list, $student);
    }
}
$json_string = json_encode($result);
exit($json_string);




